package com.gxa.weixing.portal.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.gxa.weixing.core.pojo.Order;
import com.gxa.weixing.core.pojo.OrderGoods;
import com.gxa.weixing.portal.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author HHT
 * 2022/1/12
 */
@RestController
public class PayController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/pay")
    public String pay(HttpServletRequest request) {

        String str = request.getAttribute("str").toString();
        System.out.println(str);
        String money = request.getAttribute("money").toString();
        System.out.println(money);

        System.out.println(str);
        //List<OrderGoods> orderGoods = order.getOrderGoods();
        String serverUrl = "https://openapi.alipaydev.com/gateway.do";
        String APP_ID = "2021000118687691";
        String APP_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCSPa/h9ocwSkrTNtXFviXA/hfGJuuzilDhQSQMSlIDNQOCoBVrFbDR59Vb+MUKQXmD/5xSVoaTo2VUPWVi9QSdVHZMGYxqAGohqcbKM1qYlKH+47WxUPO/D2It9MVWcIt31J65CGI3lu1WYBeRnBN8G26GTcim/3Q8FqvJAGgA1fIORPqz2nU9z68Cx3yOOq6NJBV+4Aa5rqJrcvmsFgKi0XoaErazE1/orhMQoAagBAp9J3cJf4K9pSXPLDW9re8YatXZo/FvBDJ/HkbIhfRvdqgsdISJeKHsS2AOW20Gtk+CF5GkCajSfDNCprXFjVsMVmH5zyhTPMv9UCnyNB+hAgMBAAECggEAHej3/CNL6xiGzUQ93L/TQPG3MxPqyUHU4P1NviXFOFqzaRgMyWYaUBQ2kyUCR+K0U8cH1d9UjwWwR9Z20hKC6xrHRzFCg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xsMTvVrJaYQVFKint80Eoj//m0fYMGOWFgJlmfW4ELGMsjJRQSNpqyaw==";
        String CHARSET = "UTF-8";
        String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzJv0jEh6ho7y6PstSYw43GZItikD3DwHUpZpJsEFVLmWcu38+IZa3ynIUli0N7ND4v+li3EGi3jZPHFozZ0bBMaAW3a4K10DNfDgRVWnt40u75yonGkWhkZIsRC2FEq0/ARdUZvfmQya7EuBOhIt44rs4ZouarYU8GK0qI2DD9CJaBBMiSZNHWYZ92FBgE8VIIcqeQ1YTfBjORH+MICLVeoFM3YwCGrNpcbER74fswnaZsVXCoHNxtHFIsLGf22KngZA4/QOlPrLqDPL0HTgWbkqXwQvBp3nkUu45+2IcwhrnJ5yrWm62+l97819rwt3maOT4RMoOq0vWg7/EVfHLQIDAQAB/jFCkF5g/+cUlaGk6NlVD1lYvUEnVR2TBmMagBqIanGyjNamJSh/uO1sVDzvw9iLfTFVnCLd9SeuQhiN5btVmAXkZwTfBtuhk3Ipv90PBaryQBoANXyDkT6s9p1Pc+vAsd8jjqujSQVfuAGua6ia3L5rBYCotF6GhK2sxNf6K4TEKAGoAQKfSd3CX+CvaUlzyw1va3vGGrV2aPxbwQyfx5GyIX0b3aoLHSEiXih7EtgDlttBrZPgheRpAmo0nwzQqa1xY1bDFZh+c8oUzzL/VAp8jQfoQIDAQAB";

        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
        alipayRequest.setReturnUrl("http://localhost:8081/payReturn/");

        // alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":  \"" + str + "\",      "


                +
                " \"total_amount\":\""+money+"\"," +
                " \"subject\":\"Iphone6 16G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form = "";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println();
        return form;
    }

    @RequestMapping("/payReturn")
    public String payReturn(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        Iterator<Map.Entry<String, String[]>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String[]> next = iterator.next();
            String[] value = next.getValue();

            System.out.println(Arrays.toString(value));
        }
        return request.getParameter("out_trade_no");

    }
}